package com.sgcc.pda.lib_tools_database.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.github.yuweiguocn.library.greendao.MigrationHelper;
import com.sgcc.pda.lib_tools_database.gen.*;
import com.sgcc.pda.lib_tools_database.util.DataBaseConfigTools;

import org.greenrobot.greendao.database.Database;

import java.io.IOException;

public class DBOpenHelper extends DevOpenHelper {

    private String mUserId;

    public DBOpenHelper(Context context, String name, int version, String userId) {
        super(context, name, version);
        this.mUserId = userId;
    }

    public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(Database db) {
        super.onCreate(db);
    }

    @Override
    public void onUpgrade(Database db, int oldVersion, int newVersion) {
        MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {
                    @Override
                    public void onCreateAllTables(Database db, boolean ifNotExists) {
                        DaoMaster.createAllTables(db, ifNotExists);
                    }

                    @Override
                    public void onDropAllTables(Database db, boolean ifExists) {
                        DaoMaster.dropAllTables(db, ifExists);
                    }
                },
                DeviceCodeDao.class,
                DeviceErrorDao.class,
                DeviceErrorDetailDao.class,
                DeviceInfoDao.class);
        try {
            DataBaseConfigTools.createText(mUserId, newVersion + "");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
